var util = require('../../utils/util.js');
Page({
  data: {
    disabled: false,
    columns: ['父母', '亲戚', '朋友', '其他'],
    show: false,
    realition: "",
    fileList: [],
    date: null,
    minTime: null,
  },
  onLoad: function (options) {
    // console.log(options)
    var localtime = new Date().toLocaleDateString().replace(/\//g, '-');

    this.setData({
      allID: options.allID,
      minTime: localtime,
    })

  },
  showPopup() {
    this.setData({ show: true });
  },
  onCancel() {
    this.setData({ show: false });
  },
  onConfirm(event) {
    console.log(event)
    this.setData({
      realition: event.detail.value,
      show: false
    })
  },
  img: function (e) {
    wx.chooseImage({
      success: res => {
        console.log(res.tempFilePaths[0])
        wx.cloud.uploadFile({
          cloudPath: "long-img/" + `${Math.floor(Math.random() * 10000000)}.png`,   //随机数
          filePath: res.tempFilePaths[0]
        }).then(res => {
          console.log(res.fileID)
          this.setData({
            img: res.fileID
          })
        }).catch(err => {
          console.error(err)
        })
      },
    })
  },
  bindDateChange: function (e) {
    // console.log(e.detail.value)
    var dates = e.detail.value.split("-")
    var limit = dates[0] + '/' + dates[1] + '/' + dates[2]
    var limits = Date.parse(limit) / 1000;
    // console.log(limits)
    this.setData({
      date: e.detail.value,
      limits: limits
    })
  },
  onShow: function (e) {
    // //获取时间戳
    // this.time = new Date();
    // //时间戳转换
    // console.log(this.time)
    // var date = util.formatTime(this.time);
    // var timeTamp = Date.parse(date)/ 1000;
    // this.setData({ time: timeTamp })
    // console.log(this.data)
  },
  bindSave: function (e) {
    var names = e.detail.value.names
    var phones = e.detail.value.phones
    var idcard = e.detail.value.idcard
    var realition = this.data.realition
    console.log(this.data)
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    // console.log(this.data.studentID)
    if (names == null) {
      wx.showToast({
        title: '请输入姓名！',
        icon: 'none',
        duration: 2000,
      })
      return false
    } else if (phones == null) {
      wx.showToast({
        title: '请输入手机号！',
        icon: 'none',
        duration: 2000,
      })
      return false
    } else if (!myreg.test(phones)) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'none',
        duration: 2000
      })
      return false;
    } else if (idcard == null) {
      wx.showToast({
        title: '请输入身份证号码！',
        icon: 'none',
        duration: 2000,
      })
      return false
    } else if (realition == '') {
      wx.showToast({
        title: '请选择关系！',
        icon: 'none',
        duration: 2000,
      })
      return false
    }

    
    //此云函数用于判断是否正在授权中
    wx.cloud.callFunction({
      name: "get-pro-MessageChannel",
      data: {
        phone: phones,
      }
    }).then(res => {
      // console.log(res.result)
      //未授权的情况
      if (res.result.length == 0) {
        //获取当前时间戳
        var time = Date.parse(new Date()) / 1000;
        //计算相隔小时数
        var longer = (parseInt(this.data.limits) - parseInt(time)) / 3600

        //此云函数用于授权，写入数据
        wx.cloud.callFunction({
          name: "teacher-pro",
          data: {
            names: e.detail.value.names,
            phones: phones,
            idcard: idcard,
            realition: realition,
            time:time,
            allID: this.data.allID,
            limits: this.data.limits,
            fileID: this.data.img,
            longer: longer,
          },
        }).then(res => {
          console.log(res)
          wx.showToast({
            title: '授权成功',
            icon: 'success',
            mask: true,
            duration: 1500,
            success: function () {
              setTimeout(function () { wx.navigateBack({}) }, 1000)
            },
          })
        })
      } else {
        wx.showToast({
          title: '已被授权',
          icon: 'none',
          duration: 2000,
        })
      }
    }
    )
  }
})